#include "test.h"

#include <vector>

#include "Complex.hpp"
#include "movefunc.h"
#include "sample.h"
void TestSample() {
  auto i = 1234L;
  nil02::test_vector(i);
}

void TestSwap() {
  std::vector<int> v{4, 5}, v2{1, 2, 3, 4, 5, 6};
  v.swap(v2);
  std::swap(v2, v);
  //  std::hash(v);
  for (auto iter = v.begin(); iter != v.end(); ++iter) {
    std::cout << *iter << " ";
  }
  std::cout << std::endl;
  std::for_each(v2.begin(), v2.end(), [](int i) { std::cout << i << " "; });
  std::cout << std::endl;
  std::cout << typeid(Complex).name() << std::endl;
}

void TestForward() {
  int a = 1;
  process(a);
  process(1);
  forward_fun(1);             // 不完美转发
  forward_fun(std::move(a));  // 不完美转发
                              /*
                              输出：
                              process(int&): 1
                              process(int&&): 1
                              forward_fun(int&&): 1 ;process(int&): 1
                              forward_fun(int&&): 1 ;process(int&): 1
                               */
}

void TestComplex() {
  Complex a(23, 23.82);
  Complex b(a);
  Complex c{1, 2};

  std::cout << " a=" << a << " \n b=" << b << " \n c=" << c << std::endl;

  Complex d(std::move(c));
  c = std::move(a);
  std::cout << "a=" << a << "b=" << b << "c=" << c << "d=" << d << std::endl;
  a + b = c;
  Complex() = b;
}

void TestMemory() {
  //  ::nil03::main_func();
  ::nil04::main_func();
}

void TestContainers() { ::nil05::main_func(); }

#include "mystringmap.h"
void TestMapStrStr() {
  //  using std::pair;
  //  using std::string;
  //  MyStringMap map;
  //  map.PushKeyValue("aa", "value=aaa");
  //  map.PushKeyValue("bb", "value=aaa");
  //  map.PushKeyValue("cc", "value=aaa");
  //  map.PushKeyValue("adda", "value=aaa");
  //  map.PushKeyValue("ee", "value=aaa");
  //  map.PushKeyValue("ff", "value=aaa");

  //  auto value = map.FindOrDefault("ee", "ee");
  //  std::cout << "find(ee)=" << value << std::endl;

  nilyang::func_mapstr();
}

#include <algorithm>
void TestLambda() {
  using std::map;
  using std::string;
  using std::transform;
  typedef map<int, string> value_type;
  value_type intStrMap;
  intStrMap.insert(std::make_pair(1, "hello-1"));
  intStrMap.insert(std::make_pair(23, "hello-23"));
  intStrMap.insert(std::make_pair(31, "hello-31"));
  intStrMap.insert(std::make_pair(123, "hello-123"));
  intStrMap.insert(std::make_pair(12, "hello-12"));

  intStrMap.insert(std::make_pair(4, "hello-4"));
  intStrMap.insert(std::make_pair(912, "hello-912"));
  std::cout << "=====before====\n";
  std::unordered_map<int, string> uIntStrMap;
  for (auto x = intStrMap.begin(); x != intStrMap.end(); ++x) {
    std::cout << "(" << x->first << "," << x->second << ")\n";
    uIntStrMap.insert(*x);
  }

  typedef decltype(*intStrMap.begin()) elem_type;
  std::for_each(intStrMap.begin(), intStrMap.end(),
                [](elem_type iter) -> bool { return iter.first < 10; });

  std::cout << "=====after====\n";

  for (auto x = intStrMap.begin(); x != intStrMap.end(); ++x) {
    std::cout << "(" << x->first << "," << x->second << ")\n";
  }
  // 结果说明，map是排序树

  //=====before====
  //(1,hello-1)
  //(4,hello-4)
  //(12,hello-12)
  //(23,hello-23)
  //(31,hello-31)
  //(123,hello-123)
  //=====after====
  //(1,hello-1)
  //(4,hello-4)
  //(12,hello-12)
  //(23,hello-23)
  //(31,hello-31)
  //(123,hello-123)

  std::cout << "===unorderd_map=====\n";
  for (auto& x : uIntStrMap) {
    std::cout << "(" << x.first << "," << x.second << ")\n";
  }
}

#include "mythread.h"
void TestThread() { nilyang::func_thread(); }

void TestStlAlgrithms() { nil05::cpp11_array_sort(); }

void TestMain() { TestStlAlgrithms(); }
